package com.test.ssm.article.controller;


import com.test.ssm.article.entity.pojo.ArticleType;
import com.test.ssm.article.entity.pojo.SystemFunction;
import com.test.ssm.article.entity.pojo.SystemRole;
import com.test.ssm.article.service.SystemFunctionService;
import com.test.ssm.article.service.SystemRoleService;
import com.test.ssm.article.util.helper.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@Controller
@RequestMapping("/system/role")
public class SystemRoleController {
    @Autowired
    private SystemRoleService systemRoleService;
    @GetMapping("/")
    public String getAll(@RequestParam(value = "roleState",required = false,defaultValue = "2") Byte roleState,
                         @RequestParam(value = "roleName",required = false,defaultValue = "") String roleName,
                         @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                         Map<String,Object> map){
        MyPage<SystemRole> page = systemRoleService.getAll(roleState,roleName,pageNum,pageSize);
        map.put("page",page);
        map.put("state",roleState);
        map.put("name",roleName);

        System.out.println("map = " + map);


        return "article/role";
    }

    @GetMapping("/to_add")
    public String toAdd(){
        return "article/roleadd";
    }

    @GetMapping("/check_is_exist")
    @ResponseBody
    public boolean checkIsExist(String roleName){
        System.out.println("typeName = " + roleName);
        return systemRoleService.checkIsExist(roleName) != null;
    }

    @PostMapping("/add")
    @ResponseBody
    public Boolean add(@Valid SystemRole systemRole){
        return systemRoleService.add(systemRole);
    }

    @GetMapping("/delete")
    @ResponseBody
    public Boolean delete(String roleId){
        return systemRoleService.deleteOneById(roleId) != null;
    }

    @GetMapping("/batch_delete")
    @ResponseBody
    public long batchDelete(String roleIds){
        return systemRoleService.batchDelete(roleIds);
    }

    public static String updateId;
    @GetMapping("/to_update")
    public String toUpdate(String roleId){
        updateId=roleId;
        return "article/roleupdate";
    }

    @PostMapping("/update")
    @ResponseBody
    public Boolean update(@Valid SystemRole systemRole){

        systemRole.setRoleId(updateId);
        return systemRoleService.update(systemRole);
    }
}
